/*
 * Filename H5ReportController.java 2022年05月12日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.terminal.controller;

import com.ehome.poct.base.api.dto.doctor.ClinicDTO;
import com.ehome.poct.base.api.dto.doctor.TermClinicDTO;
import com.ehome.poct.base.api.feign.ClinicClient;
import com.ehome.poct.core.api.feign.ReportClient;
import com.ehome.poct.core.api.vo.report.H5ReportDetailVO;
import com.ehome.poct.terminal.api.vo.EditDisplayOrderVO;
import com.ehome.web.annotation.RequestLog;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * H5页面报告
 *
 * @author wyc
 * @since 2022/05/12 10:25
 **/
@Api(tags = "H5版-报告管理")
@ApiSupport(author = "muqiu")
@RequestMapping("/H5Report")
@RestController
@AllArgsConstructor
public class H5ReportController {
    private final ReportClient reportClient;
    private final ClinicClient clinicClient;

    @ApiOperation("根据订单号查询血常规(单测/联测)报告详情")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/{orderNo}")
    @RequestLog
    public H5ReportDetailVO getBldReport(@PathVariable String orderNo) {
        final var report = reportClient.getH5BldDtlByOrderNo(orderNo);
        final var clinic = clinicClient.getById(report.getPatient().getClinicId());
        report.getBase().setClinicName(clinic.getName());
        return report;
    }

    @ApiOperation("根据金标仪ID查询三联测报告详情")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/oline/jby/{id}")
    public H5ReportDetailVO getOlReportFromJby(@PathVariable Long id) {
        final var report = reportClient.getH5OLDtlByJbyId(id);
        final var clinic = clinicClient.getById(report.getPatient().getClinicId());
        report.getBase().setClinicName(clinic.getName());
        return report;
    }

    @ApiOperation("获取报告展示顺序（1：优先展示结果页；2：优先展示数据页）")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/display/order/{clinicId}")
    public Integer getDisplayOrder(@PathVariable Long clinicId) {
        return Optional.ofNullable(clinicClient.getById(clinicId)).map(ClinicDTO::getDisplayOrder).orElse(1);
    }

    @ApiOperation("修改报告展示顺序")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/display/order/edit")
    public void displayOrderEdit(@RequestBody EditDisplayOrderVO vo) {
        clinicClient.update(vo.getClinicId(), new TermClinicDTO().setDisplayOrder(vo.getOrder()));
    }
}